package com.ajie.service.impl;

import com.ajie.entity.SysPermission;
import com.ajie.mapper.SysPermissionMapper;
import com.ajie.service.SysPermissionService;
import com.ajie.utils.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
public class SysPermissionServiceImpl  implements SysPermissionService {


    @Autowired
    private SysPermissionMapper permissionMapper;
    
    @Autowired
    private RedisUtil redisUtil;
    
    @Override
    public Result findPage(@RequestBody QueryInfo queryInfo) {
        log.info("该文件名是：SysPermissionServiceImpl,该方法是：findPage");
        log.info("1.开始权限数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
        PageHelper.startPage(queryInfo.getPageNumber(),queryInfo.getPageSize());
        Page<SysPermission> page = permissionMapper.findPage(queryInfo.getQueryString());
        long total = page.getTotal();
        List<SysPermission> result = page.getResult();
        log.info("查询的总条数是-->{}",total);
        log.info("分页列表-->{}",result);
        return new PageResult(total, result);
    }

    @Override
    public Result insert(SysPermission permission) {
        log.info("该文件名是：SysPermissionServiceImpl,该方法是：insert");
        permissionMapper.insert(permission);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("添加权限数据成功");
    }

    @Override
    public Result delete(Long id) {
        log.info("该文件名是：SysPermissionServiceImpl,该方法是：delete");
        permissionMapper.delete(id);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("删除权限数据成功");
    }

    @Override
    public Result update(SysPermission permission) {
        log.info("该文件名是：SysPermissionServiceImpl,该方法是：update");
        permissionMapper.update(permission);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("修改权限数据成功");
    }
    
    /**
     * 查询所有权限
     * @return
     */
    @Override
    public Result select() {
        log.info("该文件名是：SysPermissionServiceImpl,该方法是：select");
        return Result.success("查询所有权限",permissionMapper.selectAllPermission());
    }
}
